//
//  RoomsFlow.swift
//  ObjcCodable
//
//  Created by chenbo on 2025/4/27.
//

import Foundation

// MARK: - RoomsFlow
@objcMembers class RoomsFlow: NSObject, JSONCodable {
    var buttonColor: String?
    var buttonDesc: String?
    var countDown: Int?
    var cover: String?
    var createRoomToast: String?
    var createRoomType: String?
    var createStatus: Int?
    var lineIcon: String?
    var name: String?
    var title: String?
    var user: User?
    var userList: [User]?
    var voiceRoomHot: String?
    var voiceRoomIcon: String?
    var voiceRoomId: Int?
    var voiceRoomType: String?

    enum CodingKeys: String, CodingKey {
        case buttonColor = "button_color"
        case buttonDesc = "button_desc"
        case countDown = "count_down"
        case cover = "cover"
        case createRoomToast = "create_room_toast"
        case createRoomType = "create_room_type"
        case createStatus = "create_status"
        case lineIcon = "line_icon"
        case name = "name"
        case title = "title"
        case user = "user"
        case userList = "user_list"
        case voiceRoomHot = "voice_room_hot"
        case voiceRoomIcon = "voice_room_icon"
        case voiceRoomId = "voice_room_id"
        case voiceRoomType = "voice_room_type"
    }

    init(buttonColor: String?, buttonDesc: String?, countDown: Int?, cover: String?, createRoomToast: String?, createRoomType: String?, createStatus: Int?, lineIcon: String?, name: String?, title: String?, user: User?, userList: [User]?, voiceRoomHot: String?, voiceRoomIcon: String?, voiceRoomId: Int?, voiceRoomType: String?) {
        self.buttonColor = buttonColor
        self.buttonDesc = buttonDesc
        self.countDown = countDown
        self.cover = cover
        self.createRoomToast = createRoomToast
        self.createRoomType = createRoomType
        self.createStatus = createStatus
        self.lineIcon = lineIcon
        self.name = name
        self.title = title
        self.user = user
        self.userList = userList
        self.voiceRoomHot = voiceRoomHot
        self.voiceRoomIcon = voiceRoomIcon
        self.voiceRoomId = voiceRoomId
        self.voiceRoomType = voiceRoomType
    }
    
    required init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.buttonColor = try? container.decodeIfPresent(String.self, forKey: .buttonColor)
        self.buttonDesc = try? container.decodeIfPresent(String.self, forKey: .buttonDesc)
        self.countDown = try? container.decodeIfPresent(Int.self, forKey: .countDown)
        self.cover = try? container.decodeIfPresent(String.self, forKey: .cover)
        self.createRoomToast = try? container.decodeIfPresent(String.self, forKey: .createRoomToast)
        self.createRoomType = try? container.decodeIfPresent(String.self, forKey: .createRoomType)
        self.createStatus = try? container.decodeIfPresent(Int.self, forKey: .createStatus)
        self.lineIcon = try? container.decodeIfPresent(String.self, forKey: .lineIcon)
        self.name = try? container.decodeIfPresent(String.self, forKey: .name)
        self.title = try? container.decodeIfPresent(String.self, forKey: .title)
        self.user = try? container.decodeIfPresent(User.self, forKey: .user)
        self.userList = try? container.decodeIfPresent([User].self, forKey: .userList)
        self.voiceRoomHot = try? container.decodeIfPresent(String.self, forKey: .voiceRoomHot)
        self.voiceRoomIcon = try? container.decodeIfPresent(String.self, forKey: .voiceRoomIcon)
        self.voiceRoomId = try? container.decodeIfPresent(Int.self, forKey: .voiceRoomId)
        self.voiceRoomType = try? container.decodeIfPresent(String.self, forKey: .voiceRoomType)
        super.init()
    }
}

extension RoomsFlow {
    
    static func instance(jsonObject: Any?) -> Self? {
        return JSONDecodableUtil.decode(jsonObject: jsonObject)
    }
}

// MARK: - User
@objcMembers class User: NSObject, JSONCodable {
    var avatar: String?
    var message: String?
    var nickName: String?

    enum CodingKeys: String, CodingKey {
        case avatar = "avatar"
        case message = "message"
        case nickName = "nick_name"
    }

    init(avatar: String?, message: String?, nickName: String?) {
        self.avatar = avatar
        self.message = message
        self.nickName = nickName
    }
    
    required init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.avatar = try? container.decodeIfPresent(String.self, forKey: .avatar)
        self.message = try? container.decodeIfPresent(String.self, forKey: .message)
        self.nickName = try? container.decodeIfPresent(String.self, forKey: .nickName)
        super.init()
    }
}

extension User {
    
    @objc static func array(from json: Any?) -> [Any]? {
        let res = [Self].instance(jsonObject: json)
        return res
    }
}
